package com.example.androidcomplete02.fragment;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.example.androidcomplete02.R;

public class WordListFragment extends ListFragment {
	OnWordChangeListener mListener;

	public interface OnWordChangeListener {
		void onWordChanged(int index);
	}

	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			mListener = (OnWordChangeListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					"activity must implements OnWordChangeListener");
		}
	}

	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				getActivity(), R.array.word,
				android.R.layout.simple_list_item_1);
		setListAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}

	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		mListener.onWordChanged(position);
	}
}